package com.yellowpage.nsp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.nsp.data.NspLogger;
import com.yellowpage.nsp.dao.NspLoggerDao;

public class NspLoggerServiceImpl extends BaseServiceImpl implements NspLoggerService {
	  private NspLoggerDao nspLoggerDao;
	
	  public void setNspLoggerDao(NspLoggerDao nspLoggerDao) {
		  this.nspLoggerDao = nspLoggerDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public NspLogger getNspLogger(Long id){
	      return nspLoggerDao.get(id);
	  }
	  
    public List<NspLogger> getAllNspLogger(){
        return nspLoggerDao.getAll();
    }
    
    public List<Map> queryNspLoggerByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.LOG_ID , a.REQUEST_TIME , a.REQUEST_INFO , a.REQUEST_TYPE , a.REQUEST_IP , a.RESPONSE_DATA , a.RESPONSE_INFO , a.ID , a.RESPONSE_CODE    FROM NSP_LOGGER a WHERE 1=1 ")
				    			.addCondition(" and LOG_ID = :logId ")
							    			.addCondition(" and REQUEST_TIME = :requestTime ")
							    			.addCondition(" and REQUEST_INFO = :requestInfo ")
							    			.addCondition(" and REQUEST_TYPE = :requestType ")
							    			.addCondition(" and REQUEST_IP = :requestIp ")
							    			.addCondition(" and RESPONSE_DATA = :responseData ")
							    			.addCondition(" and RESPONSE_INFO = :responseInfo ")
							    			.addCondition(" and ID = :id ")
							    			.addCondition(" and RESPONSE_CODE = :responseCode ")
							    ; 		    
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    
    public void saveNspLogger(NspLogger nspLogger){
        nspLoggerDao.save(nspLogger);
    }
    
	  public void updateNspLogger(NspLogger nspLogger){
	      nspLoggerDao.update(nspLogger);
	  }
	  
	  public void delete(List deleteList){
	      nspLoggerDao.remove(deleteList);
	  }
	  
	  
	  public List<Map> countNspLogger(Map queryMap) {		
	        
			QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
					"select b.REQUEST_TYPE, a.COMPANY, a.APP_NAME, a.TYPE, a.STATUS, " +
					"       sum(b.COUNT) as 'ALLSUM',  " +
					"       sum(IF(b.RESPONSE_CODE='000000',b.COUNT,0)) as VALIDCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE!='000000',b.COUNT,0)) as INVALIDCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE='000000' and b.RESPONSE_CONTENT is null,b.COUNT,0)) as PAYUSERCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE!='000000' and b.RESPONSE_CONTENT is null,b.COUNT,0)) as BDCDATACOUNT " +
					"  from NSP_LOG_SUM b, NSP_REQUEST_TYPE a" +
					" where b.REQUEST_TYPE = a.CODE ")
					    			.addCondition(" and b.LOG_ID = :logId ")
								    			//.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) = :requestTime ")
					    						.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) >= :requestTime ")
					    						.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) <= :requestTimeEnd ")
								    			.addCondition(" and b.REQUEST_INFO = :requestInfo ")
								    			.addCondition(" and b.REQUEST_TYPE = :requestType ")
								    			.addCondition(" and b.REQUEST_IP = :requestIp ")
								    			.addCondition(" and b.RESPONSE_DATA = :responseData ")
								    			.addCondition(" and b.RESPONSE_INFO = :responseInfo ")
								    			.addCondition(" and b.ID = :id ")
								    			.addCondition(" and b.RESPONSE_CODE = :responseCode ")
								    ; 		    
			    

			sqlAssistant.setTail(" group by b.REQUEST_TYPE ");
			
			List<Map> resultList = sqlAssistant.query(
					baseDao.getConnection(), queryMap);

			return resultList;
		}
	  
	  public Map getresponseCodeMap(){
		  
		  Map dataSourceMap = new LinkedMap();
	    	
	    	QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
			" SELECT a.value as NAME, a.key as VALUE  FROM db_ndc.config a WHERE a.type = '1' ")
						    ; 	
	         
			List<Map> resultList = sqlAssistant.query(
					baseDao.getConnection(), new HashMap());
					
			
			for(int i = 0; i < resultList.size(); i++){
				dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i).get("NAME"));
			}
			
			return dataSourceMap;
		  
		  
	  }
	  
	  public Map getrequestTypeMap(){
		  
		  Map dataSourceMap = new LinkedMap();
	    	
	    	QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
			" SELECT a.COMPANY as NAME, a.CODE as VALUE  FROM NSP_REQUEST_TYPE a  ")
						    ; 	
	         
			List<Map> resultList = sqlAssistant.query(
					baseDao.getConnection(), new HashMap());
					
			
			for(int i = 0; i < resultList.size(); i++){
				dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i).get("NAME"));
			}
			
			return dataSourceMap;
		  
		  
	  }
	  
	  public List<Map> getseachInfo(Map queryMap){
		  
			QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
					"select b.REQUEST_TYPE, a.COMPANY, a.APP_NAME, a.TYPE, a.STATUS, SUBSTR(b.LOG_TIME FROM 1 FOR 10) as DATETIME , SUBSTR(b.LOG_TIME FROM 12 FOR 13) as TIME, " +
					"       sum(b.COUNT) as 'ALLSUM',  " +
					"       sum(IF(b.RESPONSE_CODE='000000',b.COUNT,0)) as VALIDCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE!='000000',b.COUNT,0)) as INVALIDCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE='000000' and b.RESPONSE_CONTENT is null,b.COUNT,0)) as PAYUSERCOUNT, " +
					"       sum(IF(b.RESPONSE_CODE!='000000' and b.RESPONSE_CONTENT is null,b.COUNT,0)) as BDCDATACOUNT " +
					"  from NSP_LOG_SUM b, NSP_REQUEST_TYPE a" +
					" where b.REQUEST_TYPE = a.CODE ")
					    			.addCondition(" and b.LOG_ID = :logId ")
					    			//.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) = :requestTime ")
		    						.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) >= :requestTime ")
		    						.addCondition(" and SUBSTR(b.LOG_TIME FROM 1 FOR 10) <= :requestTimeEnd ")
					    			.addCondition(" and b.REQUEST_INFO = :requestInfo ")
					    			.addCondition(" and b.REQUEST_TYPE = :requestType ")
					    			.addCondition(" and b.REQUEST_IP = :requestIp ")
					    			.addCondition(" and b.RESPONSE_DATA = :responseData ")
					    			.addCondition(" and b.RESPONSE_INFO = :responseInfo ")
					    			.addCondition(" and b.ID = :id ")
					    			.addCondition(" and b.RESPONSE_CODE = :responseCode ")
								    ; 		    
			    

			sqlAssistant.setTail(" group by b.LOG_TIME ");
			
			List<Map> resultList = sqlAssistant.query(
					baseDao.getConnection(), queryMap);

			return resultList;
	  }
	 
	
}	